package com.flag.liutao.modules.student.controller;


import cn.hutool.core.util.ObjectUtil;
import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.student.domain.Major;
import com.flag.liutao.modules.student.param.MajorParam;
import com.flag.liutao.modules.student.param.MajorQueryParam;
import com.flag.liutao.modules.student.service.IDepartmentService;
import com.flag.liutao.modules.student.service.IMajorService;
import com.flag.liutao.modules.student.vo.DepartmentVo;
import com.flag.liutao.modules.student.vo.MajorVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/admin/major")
@Api(tags = "专业管理")
@RequiredArgsConstructor
public class MajorController {
    private final IMajorService majorService;
    private final IDepartmentService departmentService;

    @GetMapping("page")
    @ApiOperation(value = "获取专业信息列表",notes = "专业管理：获取专业信息列表")
    @PreAuthorize("hasAuthority('sys_power:major_R')")
    public ResponseResult<Map<String,Object>> getPage(MajorQueryParam param){
        Map<String,Object> map = majorService.getPage(param.getPage(),param.getLimit(),param.getKeyword(),param.getDeptId());
        return ResponseResult.ok(map);
    }

    @GetMapping("list")
    @ApiOperation(value = "获取所有专业信息列表",notes = "专业管理：获取所有专业信息列表")
    @PreAuthorize("hasAuthority('sys_power:major_R')")
    public ResponseResult<List<DepartmentVo>> getList(){
        return ResponseResult.ok(departmentService.getDeptList(1));
    }

    @PostMapping("add")
    @ApiOperation(value="添加专业信息",notes = "专业管理：添加专业信息")
    @PreAuthorize("hasAuthority('sys_power:major_C')")
    public ResponseResult<Boolean> addMajor(@Validated @RequestBody MajorParam param){
        majorService.addMajor(param);
        return ResponseResult.ok();
    }

    @PostMapping("edit")
    @ApiOperation(value="修改专业信息",notes = "专业管理：修改专业信息")
    @PreAuthorize("hasAuthority('sys_power:major_U')")
    public ResponseResult<Boolean> editMajor(@Validated @RequestBody MajorParam param){
        if(ObjectUtil.isNull(param.getId())){
            throw new MyException("参数错误");
        }
        majorService.editMajor(param);
        return ResponseResult.ok();
    }

    @PostMapping("del")
    @ApiOperation(value = "删除专业信息",notes = "专业管理：删除专业信息")
    @PreAuthorize("hasAuthority('sys_power:major_D')")
    public ResponseResult<Boolean> delMajor(@Validated @RequestBody IdParam param){
        majorService.delMajor(param.getId());
        return ResponseResult.ok();
    }

    @PostMapping("dels")
    @ApiOperation(value = "删除专业信息",notes = "专业管理：删除专业信息")
    @PreAuthorize("hasAuthority('sys_power:major_D')")
    public ResponseResult<Boolean> delsMajor(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
            majorService.delMajor(id);
        }
        return ResponseResult.ok();
    }
}
